<?php

namespace app\admin\model;

use think\Model;

/**
 * 社群
 * @package app\admin\model
 */
class Resource extends Model
{

    /**
     * 本月社群数据
     * @return array
     */
    public static function getResourceMonth()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereMonth('create_time')
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 本月社群数据1
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getResourceMonth1($startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->where('status', '<>', -1)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 本周社群数据
     * @return array
     */
    public static function getResourceWeek()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereWeek('create_time')
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 社群总数
     * @param array $where
     * @return int
     */
    public static function countResource($where = [])
    {
        return self::where($where)
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 昨日新增社群
     * @return int
     */
    public static function yesTodayResource()
    {
        return self::whereDay('create_time', 'yesterday')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 今日新增社群
     * @return int
     */
    public static function todayResource()
    {
        return self::whereDay('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 本周新增社群
     * @return int
     */
    public static function countResourceWeek()
    {
        return self::whereWeek('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 本月新增社群
     * @return int
     */
    public static function countResourceMonth()
    {
        return self::whereMonth('create_time')
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 社群地区分布
     * @return mixed
     */
    public static function getResourceArea()
    {
        $data = self::fieldRaw("COUNT(*) nums,r.city,l.name")
            ->alias('r')
            ->join('location l', 'r.city = l.location_id', 'left')
            ->where('r.status', '<>', -1)
            ->group('r.city')
            ->order('nums', 'desc')
            ->limit(6)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 社群分类
     * @return array
     */
    public static function resourceCate()
    {
        $data = self::alias('r')
            ->fieldRaw("COUNT(r.cids) nums,r.cids,c.name,c.color")
            ->join('resource_cate c', 'r.cids = c.id', 'left')
            ->where([
                ['r.status', '<>', -1],
                ['r.cids', '>', 0],
            ])
            ->group('r.cids')
            ->order('nums', 'desc')
            ->limit(12)
//            ->fetchSql()
            ->select();
//        var_dump($data);exit;
        return $data ? $data->toArray() : [];
    }

    /**
     * 社群数量
     * @param $where
     * @return array
     */
    public static function resourceFields($where)
    {
        return self::where($where)->where('status', '<>', -1)->column('id');
    }

    /**
     * 社群列表
     * @param $where
     * @param $where1
     * @param $page
     * @param $pagesize
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getResourceList($where, $where1, $page, $pagesize)
    {
        $data = self::alias('r')
            ->join('resource_cate c', 'c.id = r.cids', 'left')
            ->join('location l', 'l.location_id = r.city', 'left')
            ->field('r.id,r.name,r.founder,r.province,r.city,r.area,r.id as rid,r.uid,r.desc,r.qrcode,r.logo,r.company,r.create_time,r.is_high,r.is_service,r.is_partner,r.status,r.sort,r.sort1,r.number,c.name as cate_name,l.name as city_name')
            ->where($where)
            ->where($where1['otherWheres'])
            ->where('r.status', '<>', -1)
            ->page($page, $pagesize)
            ->order($where1['order'])
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 统计社群列表
     * @param $where
     * @param $where1
     * @return int
     */
    public static function countResourceList($where, $where1)
    {
        return self::alias('r')
            ->join('resource_cate c', 'c.id = r.cids', 'left')
            ->join('location l', 'l.location_id = r.city', 'left')
            ->where($where)
            ->where($where1['otherWheres'])
            ->where('r.status', '<>', -1)
            ->count();
    }

    /**
     * 单社群数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findResource($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 更新社群数据
     * @param $data
     * @param $where
     * @return Resource
     */
    public static function updateResource($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 创建社群数据
     * @param $data
     * @return Resource
     */
    public static function createResource($data)
    {
        return self::create($data);
    }

}